<?php

class Monedas extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library(array(
            'session',
            'form_validation'
        ));
        $this->load->helper(array(
            'url'
        ));
        $this->load->model(array(
            'monedas_model',
            'log_model'
        ));
    }
    
    public function index() {
        $session = $this->session->all_userdata();
        if(empty($session['SID'])) {
            redirect('/usuarios/login/', 'refresh');
        }
        
        $data['monedas'] = $this->monedas_model->gets();
        
        $data['title'] = 'Monedas';
        $data['activo'] = 'monedas';
        $this->load->view('layout/header', $data);
        $this->load->view('layout/menu');
        $this->load->view('monedas/index');
        $this->load->view('layout/footer');
    }
    
    public function agregar() {
        $session = $this->session->all_userdata();
        if(empty($session['SID'])) {
            redirect('/usuarios/login/', 'refresh');
        }
        
        $this->form_validation->set_rules('moneda', 'Moneda', 'required');
        $this->form_validation->set_rules('simbolo', 'Símbolo', 'required');
        $this->form_validation->set_rules('currency', 'Currency', 'required');
        
        if($this->form_validation->run() == FALSE) {
            
        } else {
            $datos = array(
                'moneda' => $this->input->post('moneda'),
                'simbolo' => $this->input->post('simbolo'),
                'currency' => $this->input->post('currency')
            );
            
            $id = $this->monedas_model->set($datos);
            
            $log = array(
                'idusuario' => $session['SID'],
                'tabla' => 'monedas',
                'iddelatabla' => 'idmoneda',
                'idtabla' => $id,
                'texto' => 'Se agregó la moneda: '.$this->input->post('moneda').'
Símbolo: '.$this->input->post('simbolo').'
Currency: '.$this->input->post('currency')
            );
            
            $this->log_model->set($log);
            
            redirect('/monedas/', 'refresh');
        }
        
        $data['title'] = 'Agregar Moneda';
        $data['activo'] = 'monedas';
        $this->load->view('layout/header', $data);
        $this->load->view('layout/menu');
        $this->load->view('monedas/agregar');
        $this->load->view('layout/footer');
    }
    
    public function editar($idmoneda) {
        $session = $this->session->all_userdata();
        if(empty($session['SID'])) {
            redirect('/usuarios/login/', 'refresh');
        }
        
        $this->form_validation->set_rules('moneda', 'Moneda', 'required');
        $this->form_validation->set_rules('simbolo', 'Símbolo', 'required');
        $this->form_validation->set_rules('currency', 'Currency', 'required');
        
        if($this->form_validation->run() == FALSE) {
            
        } else {
            $datos = array(
                'moneda' => $this->input->post('moneda'),
                'simbolo' => $this->input->post('simbolo'),
                'currency' => $this->input->post('currency')
            );
            
            $id = $this->monedas_model->update($datos, $idmoneda);
            
            $log = array(
                'idusuario' => $session['SID'],
                'tabla' => 'monedas',
                'iddelatabla' => 'idmoneda',
                'idtabla' => $idmoneda,
                'texto' => 'Se modificó la moneda: '.$this->input->post('moneda').'
Símbolo: '.$this->input->post('simbolo').'
Currency: '.$this->input->post('currency')
            );
            
            $this->log_model->set($log);
            
            redirect('/monedas/', 'refresh');
            
        }
        
        $data['moneda'] = $this->monedas_model->get($idmoneda);
        $data['log'] = $this->log_model->get('monedas', 'idmoneda', $idmoneda);
        
        $data['title'] = 'Editar Moneda';
        $data['activo'] = 'monedas';
        $this->load->view('layout/header', $data);
        $this->load->view('layout/menu');
        $this->load->view('monedas/editar');
        $this->load->view('layout/footer');
    }
}
?>
